#!/usr/bin/python
import cpu 
import sys
import os
import time
def cpu_times(percpu=False):
    if not percpu:
        return cpu.cpu_times()
    else:
        return cpu.per_cpu_times()
_last_cpu_times = cpu_times()
_last_per_cpu_times = cpu_times(percpu=True)
def cpu_percent(interval=None, percpu=False):
    global _last_cpu_times
    global _last_per_cpu_times
    blocking = interval is not None and interval > 0.0
    def calculate(t1, t2):
        t1_all = sum(t1)
        t1_busy = t1_all - t1.idle
                                                                                                                                                     
        t2_all = sum(t2)                                                                                                                             
        t2_busy = t2_all - t2.idle                                                                                                                   
                                                                                                                                                     
        # this usually indicates a float precision issue                                                                                             
        if t2_busy <= t1_busy:                                                                                                                       
            return 0.0                                                                                                                               
                                                                                                                                                     
        busy_delta = t2_busy - t1_busy                                                                                                               
        all_delta = t2_all - t1_all                                                                                                                  
        busy_perc = (busy_delta / all_delta) * 100                                                                                                   
        return round(busy_perc, 1)                                                                                                                   
                                                                                                                                                     
    # system-wide usage                                                                                                                              
    if not percpu:                                                                                                                                   
        if blocking:                                                                                                                                 
            t1 = cpu_times()                                                                                                                         
            time.sleep(interval)                                                                                                                     
        else:                                                                                                                                        
            t1 = _last_cpu_times                                                                                                                     
        _last_cpu_times = cpu_times()                                                                                                                
        return calculate(t1, _last_cpu_times)                                                                                                        
    # per-cpu usage                                                                                                                                  
    else:                                                                                                                                            
        ret = []                                                                                                                                     
        if blocking:                                                                                                                                 
            tot1 = cpu_times(percpu=True)                                                                                                            
            time.sleep(interval)                                                                                                                     
        else:                                                                                                                                        
            tot1 = _last_per_cpu_times                                                                                                               
        _last_per_cpu_times = cpu_times(percpu=True)                                                                                                 
        for t1, t2 in zip(tot1, _last_per_cpu_times):                                                                                                
            ret.append(calculate(t1, t2))                                                                                                            
        return ret                                                                                                                                   
                   


